<!--
 * @Author: 陈永浩(cyh1231wp@163.com)
 * @Date: 2020-12-16 17:49:15
 * @LastEditors: 陈永浩(cyh1231wp@163.com)
 * @LastEditTime: 2021-02-21 10:50:47
-->
<template>
  <div class="login-container">
    <Layout>
      <Header> </Header>
      <Content class="flex flex-center">
        <div class="login-area">
          <div class="login-tabs flex flex-between">
            <div class="tab-item active">密码登录</div>
            <div class="tab-item">验证码登陆</div>
          </div>
          <div class="login-form">
            <Form ref="loginForm">
              <FormItem prop="userName">
                <Input type="text" size="large" v-model="loginForm.userName" placeholder="请输入用户名">
                </Input>
              </FormItem>
              <FormItem prop="passWord">
                <Input type="password" size="large" v-model="loginForm.passWord" placeholder="请输入密码">
                </Input>
              </FormItem>
              <FormItem>
                <div id="captcha"></div>
              </FormItem>

              <FormItem>
                <Button type="primary" size="large" long @click="handleLogin" :disabled="!canLogin">登录</Button>
              </FormItem>
            </Form>
          </div>
        </div>
      </Content>
      <Footer> </Footer>
    </Layout>
  </div>
</template>
<script>
import { geetestMixin } from "../../lib/gt.mixin";
export default {
  data() {
    return {
      canLogin: false,
      loginForm: {
        userName: "",
        passWord: "",
      },
    };
  },
  mixins: [geetestMixin],
  methods: {
    async handleLogin() {
      if (!this.geetestIsSuccess) {
        this.$baseMessage("请先进行行为验证", "error");
        return;
      }
      this.loginForm = Object.assign(
        this.loginForm,
        this.geetest.geetestSuccessData
      );
      this.$Spin.show();
      this.$store
        .dispatch("user/login", this.loginForm)
        .then(() => {
          console.log("login success");
          const routerPath =
            this.redirect === "/404" ||
            this.redirect === "/401" ||
            !this.redirect
              ? "/select_app"
              : this.redirect;
          this.$router.push(routerPath).catch((e) => {
            console.error(e);
          });
        })
        .catch((e) => {
          console.error(e);
          this.geetestReset();
          this.canLogin = false;
        })
        .finally(() => {
          this.$Spin.hide();
        });
    },
    onGeetestSuccess() {
      this.canLogin = true;
    },
  },
};
</script>
<style lang="scss" scoped>
.ivu-layout-header {
  background-color: transparent;
}
.login-area {
  min-width: 450px;
  height: 450px;
  background-color: #fff;
  margin-top: 50px;
  padding: 50px;
  border-radius: 10px;
  .login-tabs {
    margin: 10px;
    .tab-item {
      font-size: 16px;
      line-height: 24px;
      color: #323232;
      cursor: pointer;
    }
    .active {
      color: #2db7f5;
      border-bottom: #2db7f5 2px solid;
    }
  }
  .login-form {
    margin: 10px;
    margin-top: 50px;
  }
}
</style>